package com.valten;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline;

public class JedisDemo {

    private static final int COMMAND_NUM = 1000000;

    private static final String REDIS_HOST = "127.0.0.1";

    public static void main(String[] args) {

        Jedis jedis = new Jedis(REDIS_HOST, 6379);
//        withoutPipeline(jedis);
//        withPipeline(jedis);
        boolean userInfoFlag = jedis.hexists("info_appendix", "202005209004189869812147");
        if (userInfoFlag) {
            System.out.println(userInfoFlag);
            String userInfoStr = jedis.hget("info_appendix", "202005209004189869812147");
            System.out.println(userInfoStr);
        }
    }

    private static void withoutPipeline(Jedis jedis) {
        long start = System.currentTimeMillis();
        for (int i = 0; i < COMMAND_NUM; i++) {
            jedis.hset("no_pipe", String.valueOf(i), String.valueOf(i));
        }
        long end = System.currentTimeMillis();
        long cost = end - start;
        System.out.println("withoutPipeline cost : " + cost + " ms");
    }

    private static void withPipeline(Jedis jedis) {
        Pipeline pipe = jedis.pipelined();
        long start_pipe = System.currentTimeMillis();
        for (int i = 0; i < COMMAND_NUM; i++) {
            pipe.hset("pipe", String.valueOf(i), String.valueOf(i));
        }
        pipe.sync(); // 获取所有的response
        long end_pipe = System.currentTimeMillis();
        long cost_pipe = end_pipe - start_pipe;
        System.out.println("withPipeline cost : " + cost_pipe + " ms");
    }
}